package co.fitstart.admin.web.controller.credit.order;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.entity.search.CreditOrderSearch;
import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.CreditOrderService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.entity.credit.order.CreditOrder;
import co.fitstart.entity.credit.order.CreditOrderStatus;
import co.fitstart.entity.credit.product.CreditProductType;

/**
 * 
 * @author houtao
 *
 */
@Controller
@RequestMapping("/{productType}-credit-orders")
@MenuSetting("menu-credit")
public class CreditOrderController {
	
	private static final Logger logger = LoggerFactory.getLogger(CreditOrderController.class);
    
    @Autowired
    private CreditOrderService creditOrderService;
    
    /**
     * List orders by type & status
     *  
     * @param login
     * @param type
     * @param status
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-{type}-order-management")
    public ModelAndView list(@Secure Login login,
                             @PathVariable("productType") String type,
                             @RequestParam("status") String status,
                             HttpServletRequest request,
                             Page page) {
        
        ModelAndView modelAndView = new ModelAndView("credit/credit-order/" + type + "-credit-order-list");
        modelAndView.addObject("type", type);
        modelAndView.addObject("status", status);
        
        CreditProductType creditProductType = CreditProductType.aliasOf(type);
        CreditOrderStatus creditOrderStatus = CreditOrderStatus.aliasOf(status);
        List<CreditOrder> orders = creditOrderService.list(creditOrderStatus, creditProductType, page);
        
        modelAndView.addObject("orders", orders);
        
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
    }
    
    /**
     * display search view
     * 
     * @param type
     * @param status
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search(@PathVariable("productType") String type,
                               @RequestParam("status") String status) {
        
        ModelAndView modelAndView = new ModelAndView("credit/credit-order/credit-order-search");
        
        modelAndView.addObject("type", type);
        modelAndView.addObject("status", status);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param login
     * @param orderSearch
     * @param type
     * @param orderStatus
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-{type}-order-management")
    public ModelAndView onSearch(@Secure Login login,
                                 CreditOrderSearch orderSearch,
                                 @PathVariable("productType") String type,
                                 @RequestParam("status") String orderStatus,
                                 HttpServletRequest request, 
                                 Page page) {
        
        ModelAndView modelAndView = new ModelAndView("credit/credit-order/" + type + "-credit-order-list");
        modelAndView.addObject("type", type);
        modelAndView.addObject("status", orderStatus);
        
        CreditProductType creditProductType = CreditProductType.aliasOf(type);
        orderSearch.setOrderType(creditProductType);
        
        CreditOrderStatus creditOrderStatus = CreditOrderStatus.aliasOf(orderStatus);
        orderSearch.setOrderStatus(creditOrderStatus);
        
        List<CreditOrder> orders = creditOrderService.search(orderSearch, page);
        modelAndView.addObject("orders",orders);
        
        int total = creditOrderService.totalOrder(orderSearch);
        modelAndView.addObject("total",total);
        
        // add query string
        modelAndView.addObject("refer", "search");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
        
        return modelAndView;
    }
    
    /**
     * view order detail page
     * 
     * @param orderId
     * @param orderType 
     * @return
     */
    @RequestMapping(value= "/{orderId}", method = RequestMethod.GET)
    @MenuSetting("submenu-{type}-order-management")
    public ModelAndView view(@PathVariable("orderId") Long orderId, @PathVariable("productType") String type,
                             @RequestParam(name = "status", required = false) String status){
        
        ModelAndView modelAndView = new ModelAndView("credit/credit-order/credit-order-view");
        
        CreditOrder order = creditOrderService.load(CreditOrder.class, orderId);
        
        modelAndView.addObject("order",order);
        modelAndView.addObject("status", status);
        modelAndView.addObject("type", type);
        return modelAndView;
        
    }

    /**
     * Order profile view page
     * 
     * @param orderId
     * @param orderType 
     * @return
     */
    @RequestMapping(value = "/{orderId}/product", method = RequestMethod.GET, params="view")
    @MenuSetting("submenu-{type}-order-management")
    public ModelAndView productView(@PathVariable("orderId") Long orderId, @PathVariable("productType") String type){
        
        ModelAndView modelAndView = new ModelAndView("credit/credit-order/credit-order-product-view");

        CreditOrder order = creditOrderService.load(CreditOrder.class, orderId);
        
        
        modelAndView.addObject("order",order);
        modelAndView.addObject("type", type);
        return modelAndView;
    }
    
    
    /**
     * Display edit remarks
     * 
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/{orderId}/mark", method = RequestMethod.GET)
    public ModelAndView mark(@PathVariable("orderId") Long orderId){
        
        ModelAndView modelAndView = new ModelAndView("credit/credit-order/credit-order-mark");
        
        CreditOrder order = creditOrderService.load(CreditOrder.class, orderId);
        
        modelAndView.addObject("order",order);
        
        return modelAndView;
        
    }
    
    /**
     * Do edit marks
     * 
     * @param orderId
     * @param waybillCode
     * @param shippingOffice
     * @return
     */
    @RequestMapping(value = "/{orderId}/mark", method = RequestMethod.PATCH)
    public @ResponseBody String onMark(@Preloading("orderId") CreditOrder order){
        
        Result result = new Result();
        
        try{
        	
        	creditOrderService.edit(order.mark());
            
            result.success().message("发货成功");
            
        }catch(Exception e){
            
            logger.error("onMarks - ", e);
            
            result.fail().message("发货失败");
            
        }
        
        return JsonUtils.toJsonString(result);
        
    }
    
    /**
     * Display edit address
     * 
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/{orderId}/edit-address", method = RequestMethod.GET)
    public ModelAndView editAddress(@PathVariable("orderId") Long orderId){
        
        ModelAndView modelAndView = new ModelAndView("credit/credit-order/credit-order-address");
        
        CreditOrder order = creditOrderService.load(CreditOrder.class, orderId);
        
        modelAndView.addObject("order",order);
        
        return modelAndView;
        
    }
    
    /**
     * Do edit remarks
     * 
     * @param order
     * @return
     */
    @RequestMapping(value = "/{orderId}/edit-address", method = RequestMethod.PATCH)
    public @ResponseBody String onEditAddress(@Preloading("orderId") CreditOrder order){
        
        Result result = new Result();
        
        try{
        	
        	creditOrderService.edit(order);
            
            result.success().message("修改地址成功");
            
        }catch(Exception e){
            
            logger.error("onEditAddress - ", e);
            
            result.fail().message("修改地址失败");
            
        }
        
        return JsonUtils.toJsonString(result);
        
    }
    
}
